package com.googlecode.stockhamster.client.swing;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import com.googlecode.stockhamster.Constant;
import com.googlecode.stockhamster.arbiter.IArbiter;
import com.googlecode.stockhamster.utils.XFireStub;

public class ArbiterPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5211169564208808006L;

	private IArbiter arbiter;
	private String loginCode;

	private JTextField user;
	private JTextField pass;
	private JTextField cashAmount;
	private JTextField stockId;
	private JTextField price;
	private JTextField amount;

	private JLabel accountResult;
	private JLabel bankResult;
	private JLabel marketResult;
	private JTextField cashText;
	private JTable stockTable;

	private JPanel loginPanel;
	private JPanel bankPanel;
	private JPanel marketPanel;
	private JScrollPane tablePanel;

	private static final String[] stockTableColumn = { "Stock ID", "Amount" };

	public ArbiterPanel() throws MalformedURLException {
		arbiter = new XFireStub<IArbiter>(Constant.arbiterUrl, IArbiter.class)
				.getStub();

		setLayout(new GridLayout(4, 1));

		createLoginPanel();
		createBankPanel();
		createMarketPanel();
		stockTable = new JTable(new Object[0][2], stockTableColumn);
		stockTable.setShowGrid(true);
		tablePanel = new JScrollPane(stockTable);

		add(loginPanel);
		add(bankPanel);
		add(marketPanel);
		add(tablePanel);
	}

	private void createMarketPanel() {
		marketPanel = new JPanel();
		marketPanel.add(new JLabel("Stock ID:"));
		stockId = new JTextField(2);
		marketPanel.add(stockId);
		marketPanel.add(new JLabel("Price:"));
		price = new JTextField(5);
		marketPanel.add(price);
		marketPanel.add(new JLabel("Amount:"));
		amount = new JTextField(5);
		marketPanel.add(amount);

		JButton buy = new JButton("Buy");
		buy.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (arbiter.requestBuy(loginCode, Integer.parseInt(stockId
						.getText()), Long.parseLong(price.getText()), Integer
						.parseInt(amount.getText())))
					marketResult.setText("Buy: succeed");
				else
					marketResult.setText("Buy: failed");
			}
		});
		marketPanel.add(buy);
		JButton sell = new JButton("Sell");
		sell.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (arbiter.requestSell(loginCode, Integer.parseInt(stockId
						.getText()), Long.parseLong(price.getText()), Integer
						.parseInt(amount.getText())))
					marketResult.setText("Sell: succeed");
				else
					marketResult.setText("Sell: failed");
			}
		});
		marketPanel.add(sell);
		JButton getStocks = new JButton("Get Stocks");
		getStocks.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Integer sids[] = arbiter.getStockList(loginCode);
				Object content[][] = new Object[sids.length][2];
				for (int i = 0; i < sids.length; i++) {
					content[i][0] = sids[i];
					content[i][1] = arbiter.getStockAmount(loginCode, sids[i]);
				}
				stockTable = new JTable(content, stockTableColumn);
				stockTable.setShowGrid(true);
				tablePanel.setViewportView(stockTable);
			}
		});
		marketPanel.add(getStocks);

		marketResult = new JLabel();
		marketPanel.add(marketResult);
	}

	private void createBankPanel() {
		bankPanel = new JPanel();
		bankPanel.add(new JLabel("Cash amount:"));
		cashAmount = new JTextField(8);
		bankPanel.add(cashAmount);
		JButton balance = new JButton("Get balance");
		balance.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Long b = arbiter.getBalance(loginCode);
				if (b != null)
					bankResult.setText("Get balance: " + b);
				else
					bankResult.setText("Get balance: failed");
			}
		});
		bankPanel.add(balance);
		JButton cash = new JButton("Cash");
		cash.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String moneyCode = arbiter.cash(loginCode, Long
						.parseLong(cashAmount.getText()));
				if (moneyCode != null) {
					bankResult.setText("Cash: succeed");
					cashText.setText(moneyCode);
				} else
					bankResult.setText("Cash: failed");
			}
		});
		bankPanel.add(cash);
		bankResult = new JLabel();
		bankPanel.add(bankResult);
		cashText = new JTextField(15);
		bankPanel.add(cashText);
	}

	private void createLoginPanel() {
		loginPanel = new JPanel();
		loginPanel.add(new JLabel("Username:"));
		user = new JTextField(8);
		loginPanel.add(user);
		loginPanel.add(new JLabel("Password:"));
		pass = new JTextField(8);
		loginPanel.add(pass);
		JButton register = new JButton("Register");
		register.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (arbiter.open(user.getText(), pass.getText()))
					accountResult.setText("Register: Succeed.");
				else
					accountResult.setText("Register: Failed.");
			}
		});
		loginPanel.add(register);
		JButton login = new JButton("Login");
		login.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				loginCode = arbiter.login(user.getText(), pass.getText());
				if (loginCode != null)
					accountResult.setText("Login: Succeed.");
				else
					accountResult.setText("Login: Failed.");
			}
		});
		loginPanel.add(login);
		JButton close = new JButton("Close");
		close.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (arbiter.close(loginCode))
					accountResult.setText("Close: Succeed.");
				else
					accountResult.setText("Close: Failed.");
			}
		});
		loginPanel.add(close);
		accountResult = new JLabel();
		loginPanel.add(accountResult);
	}
}
